﻿Imports System.Data.OleDb

Public Class frmInHoaDon
    Public IdDonHangBan As Integer
    Private cm As New Common
    Private dtThongTinTienHang As New DataTable
    Private dtChiTietDonHangBan As New DataTable
    Private dtThongTinKhachHang As New DataTable
    Private dtNgay As New DataTable
    Private Tien As String
    Private Sub frmInHoaDon_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.WindowState = FormWindowState.Maximized
            crvHoaDon.Width = Me.Width
            crvHoaDon.Height = Me.Height
            Dim objReport As New rptHoaDon_Full
            'Lấy được chi tiết đơn hàng bán
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtChiTietDonHangBan As New DataTable
            Dim query As String
            Dim adapter As OleDb.OleDbDataAdapter
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            'Thông tin khách hàng
            Dim dtThongTinKhachHang As New DataTable
            query = "Select * From DonHangBan Where IDDonHangBan=" & IdDonHangBan
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtThongTinKhachHang)
            If dtThongTinKhachHang.Rows(0)("NhanVienTiepThi") = "Chưa có" Then
                dtThongTinKhachHang.Rows(0)("NhanVienTiepThi") = ""
            End If
            Tien = dtThongTinKhachHang.Rows(0)("Tien")
            objReport.Database.Tables("ThongTinKhachHang").SetDataSource(dtThongTinKhachHang)

            query = "Select MaSanPham,TenSanPham,TenQuyCach,SoLuong,DonGia AS DonGiaDec,Cast(ThanhTien As VarChar(50)) As ThanhTien,LoaiPhi From ChiTietDonHangBan Where IdDonHangBan=" & IdDonHangBan & " Order By LoaiPhi DESC"
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtChiTietDonHangBan)
            dtChiTietDonHangBan.Columns.Add("DonGia")

            'Upgrade To Increase Perform
            'Tính Tổng tiền hàng
            Dim TongTienHangHoa As Double = 0
            Dim TongTienGiamTru As Double = 0
            Dim TongTienHang As Double = 0
            Dim ThueSuatVAT As Double = 0
            Dim ThueVAT As Double = 0
            TongTienHangHoa = TinhTongTienHangHoa(dtChiTietDonHangBan)
            TongTienGiamTru = TinhTongTienGiamTru(dtChiTietDonHangBan)
            query = "Select ThueVAT From DonHangBan Where IdDonHangBan=" & IDDonHangBan
            dbCommand = New OleDbCommand(query, dbConn)
            ThueSuatVAT = dbCommand.ExecuteScalar()
            TongTienHang = TongTienHangHoa - TongTienGiamTru
            ThueVAT = (ThueSuatVAT * TongTienHang) / 100
            '----Cat chuyen
            'Lấy Thông Tin Tiền Hàng
            Dim dtThongTinTienHang As New DataTable
            dtThongTinTienHang = ThongTinTienDonHang()
            Dim newRow As DataRow
            newRow = dtThongTinTienHang.NewRow
            Dim TongTienThanhToan As Double = 0
            Dim sThongThanhToan As String
            TongTienThanhToan = TongTienHang + ThueVAT
            sThongThanhToan = TongTienThanhToan.ToString("N", VietNam)
            newRow.Item("TienHang") = TongTienHang.ToString("N", VietNam)
            newRow.Item("ThueGTGT") = ThueVAT.ToString("N", VietNam)
            newRow.Item("ThueSuatGTGT") = ThueSuatVAT '.ToString
            newRow.Item("TongThanhToan") = sThongThanhToan
            Dim doctien As String = ""
            If Tien = "VND" Then
                doctien = Num2Text(LamTron(TongTienThanhToan, 0))
            ElseIf Tien = "USD" Then
                'Tách ra phần USD và phần cents
                Dim Tien As String = LamTron(TongTienThanhToan, 2).ToString()
                Dim aTien() As String = Tien.Split(".")
                Dim TienChan As Integer
                Dim TienLe As Integer
                If aTien.Length = 2 Then
                    TienChan = Integer.Parse(aTien(0))
                    TienLe = Integer.Parse(aTien(1))
                    doctien = Num2Text(TienChan).Replace("đồng", "") & " USD " & Num2Text(TienLe).Replace("đồng", "").ToLower() & " cents"
                ElseIf aTien.Length = 1 Then
                    TienChan = Integer.Parse(aTien(0))
                    doctien = Num2Text(TienChan).Replace("đồng", "") & " USD"
                End If
            ElseIf Tien = "SGD" Then
                'Tách ra phần USD và phần cents
                Dim Tien As String = LamTron(TongTienThanhToan, 2).ToString()
                Dim aTien() As String = Tien.Split(".")
                Dim TienChan As Integer
                Dim TienLe As Integer
                If aTien.Length = 2 Then
                    TienChan = Integer.Parse(aTien(0))
                    TienLe = Integer.Parse(aTien(1))
                    doctien = Num2Text(TienChan).Replace("đồng", "") & " SGD " & Num2Text(TienLe).Replace("đồng", "").ToLower() & " cents"
                ElseIf aTien.Length = 1 Then
                    TienChan = Integer.Parse(aTien(0))
                    doctien = Num2Text(TienChan).Replace("đồng", "") & " SGD"
                End If
            End If

            newRow.Item("TienBangChu") = doctien
            dtThongTinTienHang.Rows.Add(newRow)
            objReport.Database.Tables("ThongTinTienDonHang").SetDataSource(dtThongTinTienHang)
            Dim DonGia As Double
            For i As Integer = 0 To dtChiTietDonHangBan.Rows.Count - 1
                If dtChiTietDonHangBan.Rows(i)("LoaiPhi") = "GiamTru" Then
                    DonGia = Double.Parse(dtChiTietDonHangBan.Rows(i)("DonGiaDec"))
                    If DonGia <= 100 Then
                        dtChiTietDonHangBan.Rows(i)("TenSanPham") = dtChiTietDonHangBan.Rows(i)("MaSanPham") & " (" & DonGia & "%)"
                    Else
                        dtChiTietDonHangBan.Rows(i)("TenSanPham") = dtChiTietDonHangBan.Rows(i)("MaSanPham")
                    End If
                    dtChiTietDonHangBan.Rows(i)("DonGia") = ""
                Else
                    dtChiTietDonHangBan.Rows(i)("DonGia") = Convert.ToDouble(dtChiTietDonHangBan.Rows(i)("DonGiaDec")).ToString("N", VietNam).ToString()
                End If
                'format dongia
                dtChiTietDonHangBan.Rows(i)("ThanhTien") = Convert.ToDouble(dtChiTietDonHangBan.Rows(i)("ThanhTien")).ToString("N", VietNam).ToString()
            Next
            ''Phát Sinh các row trắng
            Dim emptyRow As DataRow
            For i As Integer = 1 To 12 - dtChiTietDonHangBan.Rows.Count
                emptyRow = dtChiTietDonHangBan.NewRow
                dtChiTietDonHangBan.Rows.Add(emptyRow)
                'dtChiTietDonHangBan.AcceptChanges()
            Next
            objReport.Database.Tables("ChiTietDonHangBan").SetDataSource(dtChiTietDonHangBan)
            'Thông Tin Ngày Hóa Đơn 
            Dim dsNgay As New dsNgay
            Dim dtNgay As New DataTable
            dtNgay = dsNgay.Tables(0)
            Dim dateRow As DataRow
            dateRow = dtNgay.NewRow
            dateRow.Item("Ngay") = dtpNgayHopDong.Value.ToShortDateString()
            formatDateVN2String_In(dateRow.Item("Ngay"))
            dtNgay.Rows.Add(dateRow)
            objReport.Database.Tables("dtNgay").SetDataSource(dtNgay)
            crvHoaDon.ReportSource = objReport
        Catch ex As Exception

        End Try
    End Sub
    'Hàm Tính Tổng Tiền Hàng Hóa Trong Hóa Đơn
    Private Function TinhTongTienHangHoa(ByVal dtSanPham As DataTable) As Double
        Try
            Dim sRet As Double = 0
            Dim rowHangHoa() As DataRow
            rowHangHoa = dtSanPham.Select("LoaiPhi='HangHoa'")
            For i As Integer = 0 To rowHangHoa.Length - 1
                sRet = sRet + rowHangHoa(i).Item("ThanhTien")
            Next
            Return sRet
        Catch ex As Exception
            ShowError()
        End Try
    End Function
    'Hàm Tính Tổng Tiền Giảm Trừ Trong Hóa Đơn
    Private Function TinhTongTienGiamTru(ByVal dtSanPham As DataTable) As Double
        Try
            Dim sRet As Double = 0
            Dim rowGiamTru() As DataRow
            rowGiamTru = dtSanPham.Select("LoaiPhi='GiamTru'")
            For i As Integer = 0 To rowGiamTru.Length - 1
                'sRet = sRet + Single.Parse(rowGiamTru(i).Item("ThanhTien").ToString, Globalization.NumberStyles.Currency)
                sRet = sRet + rowGiamTru(i).Item("ThanhTien")
            Next
            Return sRet
        Catch ex As Exception
            ShowError()
        End Try
    End Function
    Function ConvertCurencyToInt(ByVal Curency As String) As Single
        Dim retInt As Single
        Curency = Curency.Split(",")(0).Replace(".", "")
        retInt = Single.Parse(Curency)
        Return retInt
    End Function
    '----------Tạo Table Sản Phẩm-------------
    Protected Function ThongTinTienDonHang() As DataTable
        Dim dtTable As DataTable = New DataTable()
        Try

            Dim TienHang As DataColumn = New DataColumn("TienHang")
            TienHang.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(TienHang)

            Dim ThueSuatGTGT As DataColumn = New DataColumn("ThueSuatGTGT")
            ThueSuatGTGT.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(ThueSuatGTGT)

            Dim ThueGTGT As DataColumn = New DataColumn("ThueGTGT")
            ThueGTGT.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(ThueGTGT)

            Dim TongThanhToan As DataColumn = New DataColumn("TongThanhToan")
            TongThanhToan.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(TongThanhToan)

            Dim TienBangChu As DataColumn = New DataColumn("TienBangChu")
            TienBangChu.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(TienBangChu)
            Return dtTable
        Catch ex As Exception
            Throw
        End Try
    End Function

    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click
        Try
            Order_Print()
        Catch ex As Exception

        End Try
    End Sub

    
    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        ' if it is a hotkey, return true; otherwise, return false
        Select Case keyData
            Case Keys.Control Or Keys.P
                'In hóa đơn
                Order_Print()
                Return True
            Case Else
                Exit Function
        End Select
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

    Private Sub Order_Print()
        Try
            Dim objReport As New rptHoaDon
            objReport.Database.Tables("ThongTinTienDonHang").SetDataSource(dtThongTinTienHang)
            objReport.Database.Tables("ChiTietDonHangBan").SetDataSource(dtChiTietDonHangBan)
            objReport.Database.Tables("ThongTinKhachHang").SetDataSource(dtThongTinKhachHang)
            'Thông Tin Ngày Hóa Đơn 
            Dim dsNgay As New dsNgay
            Dim dtNgay As New DataTable
            dtNgay = dsNgay.Tables(0)
            Dim dateRow As DataRow
            dateRow = dtNgay.NewRow
            dateRow.Item("Ngay") = dtpNgayHopDong.Value.ToShortDateString()
            formatDateVN2String_In(dateRow.Item("Ngay"))
            dtNgay.Rows.Add(dateRow)
            objReport.Database.Tables("dtNgay").SetDataSource(dtNgay)
            'set font size
            objReport.PrintOptions.PrinterName = Printer()
            objReport.PrintToPrinter(1, False, 1, 1)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub dtpNgayHopDong_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpNgayHopDong.ValueChanged
        Me.WindowState = FormWindowState.Maximized
        crvHoaDon.Width = Me.Width
        crvHoaDon.Height = Me.Height
        Dim objReport As New rptHoaDon_Full
        objReport.Database.Tables("ThongTinTienDonHang").SetDataSource(dtThongTinTienHang)
        objReport.Database.Tables("ChiTietDonHangBan").SetDataSource(dtChiTietDonHangBan)
        objReport.Database.Tables("ThongTinKhachHang").SetDataSource(dtThongTinKhachHang)
        'Ngay
        Dim dsNgay As New dsNgay
        Dim dtNgay As New DataTable
        dtNgay = dsNgay.Tables(0)
        Dim dateRow As DataRow
        dateRow = dtNgay.NewRow
        dateRow.Item("Ngay") = dtpNgayHopDong.Value.ToShortDateString()
        formatDateVN2String_print(dateRow.Item("Ngay"))
        dtNgay.Rows.Add(dateRow)
        objReport.Database.Tables("dtNgay").SetDataSource(dtNgay)
        crvHoaDon.ReportSource = objReport
    End Sub
End Class